package com.mask.im.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mask.im.business.model.entity.FeedbackType;
import com.mask.im.business.mapper.FeedbackTypeMapper;
import com.mask.im.business.model.dto.FeedbackTypeDTO;
import com.mask.im.business.model.vo.ConditionVO;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.service.FeedbackTypeService;
import com.mask.im.business.common.util.MaskIMSecurityUtils;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author jian
 */
@Service
public class FeedbackTypeServiceImpl extends ServiceImpl<FeedbackTypeMapper, FeedbackType> implements FeedbackTypeService {

	@Override
	public RestResponse<List<FeedbackType>> getFeedbackType() {
		List<FeedbackType> feedbackTypes = this.baseMapper.selectList(
				new LambdaQueryWrapper<FeedbackType>().select(
						FeedbackType::getId,
						FeedbackType::getName,
						FeedbackType::getSort)).stream()
				.sorted(Comparator.comparing(FeedbackType::getSort))
				.collect(Collectors.toList());

		return RestResponse.ok(feedbackTypes);
	}

	@Override
	public RestResponse<List<FeedbackTypeDTO>> feedbackTypeList(ConditionVO conditionVO) {
		List<FeedbackTypeDTO> feedbackTypeDTOS =
				this.baseMapper.feedbackTypeList(conditionVO.getKeywords());
		return RestResponse.ok(feedbackTypeDTOS);
	}

	@Override
	public RestResponse<String> addFeedbackType(FeedbackType feedbackType) {
		feedbackType.setUpdateUser(MaskIMSecurityUtils.getUserId());
		int insert = this.baseMapper.insert(feedbackType);
		if (insert>0){
			return RestResponse.ok();
		}
		return RestResponse.fail();
	}

	@Override
	public RestResponse<String> updateFeedbackType(FeedbackType feedbackType) {
		FeedbackType feedbackTypeItem = this.getById(feedbackType.getId());
		feedbackType.setUpdateUser(MaskIMSecurityUtils.getUserId());
		if (feedbackTypeItem!=null){
			feedbackType.setPreTypeName(feedbackTypeItem.getName());
			this.updateById(feedbackType);
		}else {
			this.save(feedbackType);
		}
		return RestResponse.ok();
	}

	@Override
	public RestResponse<String> deleteFeedBackType(Integer id) {
		boolean remove = this.removeById(id);
		if (remove){
			return RestResponse.ok();
		}
		return RestResponse.fail();
	}
}

